package org.sakai.service.impl;

import org.sakai.entity.Doctor;
import org.sakai.entity.ErrorCode;
import org.sakai.entity.Result;
import org.sakai.entity.dto.DoctorQueryDto;
import org.sakai.entity.vo.DoctorVo;
import org.sakai.mapper.DoctorMapper;
import org.sakai.service.DoctorService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

@Service
public class DoctorServiceImpl implements DoctorService {
    private static final Logger logger = LoggerFactory.getLogger(DoctorServiceImpl.class);

    @Autowired
    private DoctorMapper doctorMapper;

    @Override
    public Result<List<DoctorVo>> getDoctorList(DoctorQueryDto queryDto) {
        try {
            // Log query parameters
            logger.info("Querying doctors with parameters: {}", queryDto);
            
            // Ensure pageNum starts from 1
            if (queryDto.getPageNum() == null || queryDto.getPageNum() < 1) {
                queryDto.setPageNum(1);
            }
            if (queryDto.getPageSize() == null || queryDto.getPageSize() < 1) {
                queryDto.setPageSize(10);
            }
            
            // Calculate offset in Java
            int offset = (queryDto.getPageNum() - 1) * queryDto.getPageSize();
            queryDto.setOffset(offset);
            
            // 调用mapper查询医生列表
            List<Doctor> doctors = doctorMapper.selectDoctorList(queryDto);
            
            // Log query results
            logger.info("Found {} doctors", doctors != null ? doctors.size() : 0);
            
            if (doctors == null || doctors.isEmpty()) {
                return Result.success(new ArrayList<>());
            }
            
            // 转换为VO对象
            List<DoctorVo> doctorVos = new ArrayList<>();
            for (Doctor doctor : doctors) {
                DoctorVo vo = new DoctorVo();
                BeanUtils.copyProperties(doctor, vo);
                // TODO: 需要补充其他字段的转换逻辑，如科室列表、评分、接诊数等
                doctorVos.add(vo);
            }
            
            return Result.success(doctorVos);
        } catch (Exception e) {
            logger.error("Error querying doctor list", e);
            return Result.error(ErrorCode.UNKNOWN_ERROR);
        }
    }

    @Override
    public Result<DoctorVo> getDoctorDetail(String id) {
        try {
            // 调用mapper查询医生详情
            Doctor doctor = doctorMapper.selectDoctorById(id);
            if (doctor == null) {
                return Result.error(ErrorCode.USER_NOT_FOUND_ERROR);
            }
            
            // 转换为VO对象
            DoctorVo vo = new DoctorVo();
            BeanUtils.copyProperties(doctor, vo);
            // TODO: 需要补充其他字段的转换逻辑，如科室列表、评分、接诊数等
            
            return Result.success(vo);
        } catch (Exception e) {
            logger.error("Error getting doctor detail for id: {}", id, e);
            return Result.error(ErrorCode.UNKNOWN_ERROR);
        }
    }
} 